package com.fBlog.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.fBlog.dao.impl.FamilyDaoI;
import com.fBlog.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fBlog.dao.impl.UserDaoI;


@Service
public class UserServiceImpl implements UserServiceI{

	@Autowired
	private UserDaoI userDao;
	@Autowired
	private FamilyDaoI familyDao;
	
	@Override
	public User login(String familyName,String name, String pwd) {
		Tuser t = userDao.get("from Tuser t where t.family.name = '"+familyName+"' and t.userName = '"+name+"' and t.password = '"+pwd+"' order by t.createTime");
		if (t != null) {
			User p = new User();
			BeanUtils.copyProperties(t, p);
			if(t.getFamily()!=null){
				p.setFamilyId(t.getFamily().getId());
				p.setFamilyName(t.getFamily().getName());
				p.setFamilyCreateTime(t.getFamily().getCreateTime());
			}
			return p;
		}
		return null;
	}

	@Override
	public List<User> getUsers(String familyId) {
		List<User> l = new ArrayList<User>();
		List<Tuser> t = userDao.find("from Tuser t where t.family.id = '"+familyId+"' order by t.createTime");
		if(t!=null && t.size()>0){
			for(Tuser a : t){
				User ap = new User();
				BeanUtils.copyProperties(a, ap);
				ap.setFamilyId(a.getFamily().getId());
				ap.setFamilyName(a.getFamily().getName());
				ap.setFamilyCreateTime(a.getFamily().getCreateTime());
				l.add(ap);
			}
		}
		return l;
	}

	@Override
	public String regFamily(String familyName, String admin, String password) {
		String code = "";
		String id = UUID.randomUUID().toString();
		String userId = UUID.randomUUID().toString();
		//查询该家庭名称是否存在
		Tfamily f = familyDao.get("from Tfamily t where t.name = '"+familyName+"'");
		if(f!=null){
			code = "1";
		}else{
			//注册家庭信息
			familyDao.executeSql("insert into tfamily (id,level,createTime,name) values ('"+id+"',0,now(),'"+familyName+"')");
			//注册家庭admin用户信息
			userDao.executeSql("insert into tuser (id,role,userName,password,createTime,FAMILYID) values ('"+userId+"','admin','"+admin+"','"+password+"',now(),'"+id+"')");
			code = "0";
		}
		return code;
	}

	@Override
	public String saveUser(String familyId,String userName, String role, String password) {
		String code = "";
		String id = UUID.randomUUID().toString();
		//首先查询该家庭下是否已存在相同的用户名
		Tuser user = userDao.get("from Tuser t where t.family.id = '"+familyId+"' and t.userName = '"+userName+"'");
		if(user!=null){
			code = "1";
		}else{
//			Tuser u = new Tuser();
//			u.setCreateTime(new Date());
//			u.setFamily(familyDao.get(Tfamily.class,familyId));
//			u.setId(UUID.randomUUID().toString());
//			u.setUserName(userName);
//			u.setRole(role);
//			u.setPassword(password);
//			userDao.save(u);
			userDao.executeSql("insert into tuser (id,role,userName,password,createTime,FAMILYID) values ('"+id+"','"+role+"','"+userName+"','"+password+"',now(),'"+familyId+"')");
			code = "0";
		}
		return code;
	}

	public String updateUser(String userId,String familyId,String userName, String role, String password) {
		String code = "";
		//首先查询该家庭下是否已存在相同的用户名
		Tuser user = userDao.get("from Tuser t where t.id != '"+userId+"' and t.family.id = '"+familyId+"' and t.userName = '"+userName+"'");
		if(user!=null){
			code = "1";
		}else{
			userDao.executeSql("update tuser set role = '"+role+"',userName = '"+userName+"',password = '"+password+"' where id = '"+userId+"'");
			code = "0";
		}
		return code;
	}
}
